/**
 *
 */
package com.hexacta.hrs.services.staffing.impl.predicates;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.vectrics.employee.Employee;

import com.hexacta.hrs.domain.employee.history.CareerRole;
import com.hexacta.hrs.domain.helper.EmployeeHelper;

/**
 * @author ldeveikis
 *
 */
public class EmployeeRolesPredicate implements Predicate {

	private List<String> employeeRoles;
	private Date from;
	private Date to;

	private EmployeeRolesPredicate() {
	}

	public EmployeeRolesPredicate(List<String> roles, Date from, Date to) {
		this.employeeRoles = roles;
		this.from = from;
		this.to = to;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.apache.commons.collections.Predicate#evaluate(java.lang.Object)
	 */
	@Override
	public boolean evaluate(Object object) {
		if (!(object instanceof Employee)) {
			return false;
		} else {
			Employee empl = (Employee) object;
			// Buscamos los roles entre las fechas dadas
			Collection<CareerRole> careerRoles = EmployeeHelper
					.findRolesBetweenDates(empl, this.from, this.to);
			List<String> roleCodes = new ArrayList<String>();
			// Obtenemos los codes de cada uno de los roles
			for (CareerRole role : careerRoles) {
				roleCodes.add(role.getCareerRole().getCode());
			}
			return CollectionUtils.containsAny(this.employeeRoles, roleCodes);
		}
	}

}
